package com.ktjy.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktjy.pojo.Leaverecords;
import com.ktjy.service.ILeaverecordsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hjy
 * @since 2025-01-07
 */
@Controller
@RequestMapping("/leaverecords")
public class LeaverecordsController extends BaseController {
    Logger logger= LoggerFactory.getLogger(LeaverecordsController.class);

   @Autowired
   ILeaverecordsService leaverecordsService;

   @RequestMapping("/page")
   public String page(Leaverecords leaverecords,
                      @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                      @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
                      Model model) {
       try {
           Page page = new Page(pageNo, pageSize);
           Page<Leaverecords> pageInfo = leaverecordsService.page(page, null);
           model.addAttribute("pageInfo", pageInfo);
           //查询数据条件回显
           model.addAttribute("leaverecords", leaverecords);
       }catch (Exception e) {
           logger.error("LeaverecordsController 中的 page 方法执行发生错误。{} ",e);
           throw new RuntimeException(e);
       }
       return "page";
   }

   @GetMapping("/save")
    public String tosave(Model model) {
       model.addAttribute("leaverecords",new Leaverecords());
       return "save";
   }

   @PostMapping("/save")
    public String dosave(Leaverecords leaverecords){
       boolean ok=false;
       try {
           if (leaverecords.getId()==null){
               ok=leaverecordsService.save(leaverecords);
           }else {
               ok=leaverecordsService.updateById(leaverecords);
           }
           if (ok){
               return redirect("page");
           }
       }catch (Exception e) {
           logger.error("LeaverecordsController 中的 dosave 方法执行发生错误。{} ",e);
           throw new RuntimeException(e);
       }
       return "save";
   }

   //点击修改按钮显示原本的信息
   @GetMapping("/edit")
    public String toedit(Model model,@RequestParam("id") Integer id) {
        try {
            Leaverecords leaverecords=leaverecordsService.getById(id);
            model.addAttribute("leaverecords",leaverecords);
        }catch (Exception e) {
            logger.error("LeaverecordsController 中的 edit 方法执行发生错误。{} ",e);
            throw new RuntimeException(e);
        }
        return "save";
   }

    //点击修改按钮显示原本的信息
    @GetMapping("/view/{id}")
    public String view(Model model,@PathVariable("id") Integer id) {
        try {
            Leaverecords leaverecords=leaverecordsService.getById(id);
            model.addAttribute("leaverecords",leaverecords);
        }catch (Exception e) {
            logger.error("LeaverecordsController 中的 view 方法执行发生错误。{} ",e);
            throw new RuntimeException(e);
        }
        return "view";
    }

    @DeleteMapping("/del/{id}")
    @ResponseBody
    public Object del(@PathVariable("id") Integer id) {
        Map<String,Object> map=new HashMap<>();
        try {
            boolean ok=leaverecordsService.removeById(id);
            if (ok){
                map.put("code",200);
                map.put("msg","删除成功");
            }else {
                map.put("code",500);
                map.put("msg","删除失败");
            }
        }catch (Exception e) {
            logger.error("LeaverecordsController 中的 del 方法执行发生错误。{} ",e);
            map.put("code",500);
            map.put("msg","删除失败");
        }
        return map;
    }
}
